package com.gun3y.entity.pattern;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.htmlparser.Node;

import com.gun3y.entity.media.PropertyType;
import com.gun3y.entity.result.ResultField;
import com.gun3y.utils.Commons;
import com.gun3y.utils.HtmlUtil;

public enum FieldType {
    Text {
	@Override
	public List<ResultField> extractField(Node node, FieldPattern fieldPattern, String base) {
	    String text = HtmlUtil.extractText(node, fieldPattern.getTagAttribute());
	    return createResultField(fieldPattern.getFieldName(), text, fieldPattern.getFieldType());
	}
    },
    Date {
	@Override
	public List<ResultField> extractField(Node node, FieldPattern fieldPattern, String base) {
	    String dateVal = HtmlUtil.extractDate(node, fieldPattern.getTagAttribute(), fieldPattern.getExpressions());
	    return createResultField(fieldPattern.getFieldName(), dateVal, fieldPattern.getFieldType());
	}
    },
    Number {
	@Override
	public List<ResultField> extractField(Node node, FieldPattern fieldPattern, String base) {
	    String number = HtmlUtil.extractText(node, fieldPattern.getTagAttribute());
	    number = Commons.extractNumber(number);
	    return createResultField(fieldPattern.getFieldName(), number, fieldPattern.getFieldType());
	}
    },
    Link {
	@Override
	public List<ResultField> extractField(Node node, FieldPattern fieldPattern, String base) {
	    String link = HtmlUtil.extractFrom(node, fieldPattern.getTagAttribute(), HtmlUtil.LINKATTR, base);
	    return createResultField(fieldPattern.getFieldName(), link, fieldPattern.getFieldType());
	}
    },
    Image {
	@Override
	public List<ResultField> extractField(Node node, FieldPattern fieldPattern, String base) {
	    String image = HtmlUtil.extractFrom(node, fieldPattern.getTagAttribute(), HtmlUtil.SOURCEATTR, base);
	    return createResultField(fieldPattern.getFieldName(), image, fieldPattern.getFieldType());
	}
    },
    Custom {
	@Override
	public List<ResultField> extractField(Node node, FieldPattern fieldPattern, String base) {
	    List<String> customFields = HtmlUtil.extractCustom(node, fieldPattern.getTagAttribute(), fieldPattern.getExpressions());
	    return createResultField(fieldPattern.getFieldName(), customFields, fieldPattern.getFieldType());
	}
    };

    public static FieldType getResultFieldType(String str) {
	if (StringUtils.isNotBlank(str)) {
	    try {
		return Enum.valueOf(FieldType.class, str);
	    }
	    catch (Exception e) {
		return FieldType.Text;
	    }
	}
	return FieldType.Text;
    }

    public abstract List<ResultField> extractField(Node node, FieldPattern fieldPattern, String base);

    private static List<ResultField> createResultField(PropertyType name, String value, FieldType fieldType) {
	ResultField resultField = new ResultField(name, value, fieldType);

	if (resultField.validate())
	    return Arrays.asList(resultField);
	else
	    return Arrays.asList();
    }

    private static List<ResultField> createResultField(PropertyType name, List<String> valueList, FieldType fieldType) {
	if (valueList != null) {
	    List<ResultField> fields = new ArrayList<ResultField>();
	    for (String value : valueList) {
		ResultField resultField = new ResultField(name, value, fieldType);
		if (resultField.validate())
		    fields.add(resultField);
	    }

	    return fields;
	}

	return Arrays.asList();
    }

}
